<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>流程编排-任务模板-新建</title>

    <link rel="stylesheet" href="../../plugins/fontawesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../../plugins/overlayScrollbars/css/OverlayScrollbars.min.css">
    <link rel="stylesheet" href="../../plugins/select2/css/select2.min.css">
    <link rel="stylesheet" href="../../plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css">
    <link rel="stylesheet" href="../../plugins/codemirror/codemirror.css">
    <link rel="stylesheet" href="../../plugins/codemirror/theme/darcula.css">
    <link rel="stylesheet" href="../../plugins/codemirror/addon/hint/show-hint.css">
    <link rel="stylesheet" href="../../plugins/ion-rangeslider/css/ion.rangeSlider.min.css">
    <link rel="stylesheet" href="../../plugins/bootstrap-slider/css/bootstrap-slider.min.css">
    <link rel="stylesheet" href="../../adminLTE/css/adminlte.min.css">
    <link rel="stylesheet" href="../css/cloud.css">
    <style>
        #task-arg > .row:not(:first-child) {
            margin-top: 15px;
        }

        #task-node .task-node-bash-stdout-list > .row:not(:first-child) {
            margin-top: 15px;
        }

        #task-node .task-node-sql-stdout-list > .row:not(:first-child) {
            margin-top: 15px;
        }
    </style>
</head>
<body class="content-wrapper" style="padding: 15px">
<!--基本信息-->
<div class="card card-outline card-info">
    <div class="card-header">
        <h3 class="card-title"><i class="fa fa-commenting"></i> 基本信息</h3>
    </div>
    <div id="base-info" class="card-body">
        <input type="hidden" id="id" >
        <div class="form-group row">
            <label for="name" class="col-md-1 col-form-label">名称</label>
            <div class="col-md-5"><input type="text" class="form-control" id="name" placeholder="请输入模板名称" ></div>
        </div>
        <div class="form-group row">
            <label for="count" class="col-md-1 col-form-label" style="line-height: 60px">服务器</label>
            <div class="col-md-5" title="0表示不限制">
                <input type="number" class="slider form-control" id="count">
            </div>
        </div>
    </div>
</div>
<!--模板参数-->
<div class="card card-outline card-info">
    <div class="card-header">
        <h3 class="card-title" style="margin-top: 4px"><i class="fa fa-modx"></i> 模板参数</h3>
        <div class="card-tools">
            <button id="task-arg-add" type="button" class="btn btn-sm btn-outline-info"><i class="fa fa-plus"></i> 新增</button>
        </div>
    </div>
    <div id="task-arg" class="card-body">
        <div class="row task-arg-item" data-task-arg-id="{{task-arg-id}}" data-task-arg-index="{{task-arg-index}}">
            <div class="col-md-3">
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text bg-info">类型</span>
                    </div>
                    <select class="form-control custom-select task-arg-type" ></select>
                </div>
            </div>
            <div class="col-md-3">
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text bg-info">名称</span>
                    </div>
                    <input type="text" class="form-control task-arg-name" >
                </div>
            </div>
            <div class="col-md-3">
                <div class="input-group">
                    <div class="input-group-prepend">
                        <span class="input-group-text bg-info">描述</span>
                    </div>
                    <input type="text" class="form-control  task-arg-description" >
                </div>
            </div>
            <div class="col-md-2">
                <button type="button" class="btn btn-outline-danger task-arg-del"><i class="fa fa-times mr-1"></i> 删除</button>
            </div>
        </div>
    </div>
</div>
<!--任务节点-->
<div id="task-node">
    <div class="card card-outline card-info task-node-item" data-task-node-id="{{task-node-id}}" data-task-node-index="{{task-node-index}}">
        <div class="card-header">
            <h3 class="card-title" style="margin-top: 4px">
                <i class="fa fa-tasks"></i>
                任务节点 # <span class="task-node-item-index">{{task-node-index}}</span>
            </h3>
            <div class="card-tools">
                <button type="button" class="btn btn-sm btn-outline-danger task-node-delete">
                    <i class="fa fa-times"></i> 删除
                </button>
            </div>
        </div>
        <div class="card-body">
            <!--任务名称-->
            <div class="form-group row">
                <label class="col-md-1 col-form-label">任务名称</label>
                <div class="col-md-5">
                    <input type="text" class="form-control task-node-name" placeholder="请输入任务名称">
                </div>
            </div>
            <!--任务类型-->
            <div class="form-group row">
                <label class="col-md-1 col-form-label">任务类型</label>
                <div class="col-md-5">
                    <div class="select2-info">
                        <select class="select2 task-node-type" data-dropdown-css-class="select2-info"
                                data-placeholder="请选择任务类型" style="width: 100%;"></select>
                    </div>
                </div>
            </div>
            <!--如果任务类型是脚本-->
            <div class="task-node-bash" style="display: none">
                <!--任务目标-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">任务目标</label>
                    <div class="col-md-5">
                        <div class="select2-info">
                            <select class="select2 task-node-bash-target" data-dropdown-css-class="select2-info"
                                    data-placeholder="请选择任务目标服务器" style="width: 100%;"></select>
                        </div>
                    </div>
                </div>
                <!--选择脚本-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">执行脚本</label>
                    <div class="col-md-5">
                        <div class="select2-info">
                            <select class="select2 task-node-bash-script" data-dropdown-css-class="select2-info"
                                    data-placeholder="请选择执行脚本" style="width: 100%;"></select>
                        </div>
                    </div>
                </div>
                <!--脚本预览-->
                <div class="form-group row task-node-bash-text" style="display: none">
                    <label class="col-md-1 col-form-label"></label>
                    <div class="col-md-11"><textarea></textarea></div>
                </div>
                <!--脚本参数-->
                <div class="task-node-bash-stdin" style="display: none">
                    <div class="form-group row task-node-bash-stdin-item" data-task-node-bash-stdin-id="{{script-arg-id}}">
                        <label class="col-md-1 col-form-label">参数 {{script-arg-name}}</label>
                        <div class="col-md-5">
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <div class="btn-group btn-group-toggle task-node-bash-stdin-type" data-toggle="buttons">
                                        <label style="font-weight: normal" class="btn bg-info" data-task-node-bash-stdin-type-name="{{script-arg-type-name}}">
                                            <input type="radio" name="{{task-node-id}}-{{script-arg-id}}" autocomplete="off"> {{script-arg-type-value}}
                                        </label>
                                    </div>
                                    <input class="form-control task-node-bash-stdin-input" style="width: 300px; display: none">
                                    <select class="form-control task-node-bash-stdin-select custom-select" style="width: 300px; display: none"></select>
                                </div>
                            </div>
                        </div>
                        <label class="col-md-6 col-form-label text-gray font-weight-light">{{script-arg-description}}</label>
                    </div>
                </div>
                <!--输出类型-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">输出类型</label>
                    <div class="col-md-5">
                        <div class="select2-info">
                            <select class="select2 task-node-bash-stdout-type" data-dropdown-css-class="select2-info"
                                    data-placeholder="请选择输出类型" style="width: 100%;"></select>
                        </div>
                    </div>
                </div>
                <!--结果解析-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">结果解析</label>
                    <div class="col-md-5">
                        <button type="button" class="btn btn-outline-info task-node-bash-stdout-add">
                            <i class="fa fa-plus"></i> 新增
                        </button>
                    </div>
                </div>
                <!--结果解析列表-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label"></label>
                    <div class="col-md-11 task-node-bash-stdout-list">
                        <div class="row task-node-bash-stdout-item" data-task-node-bash-stdout-index="{{task-node-bash-stdout-index}}">
                            <div class="col-md-4">
                                <div class="input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text bg-info">模板参数</span>
                                    </div>
                                    <select class="form-control custom-select task-node-bash-stdout-select"></select>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="input-group">
                                    <div class="input-group-prepend">
                                        <span class="input-group-text bg-info">表达式</span>
                                    </div>
                                    <input type="text" class="form-control task-node-bash-stdout-expression">
                                </div>
                            </div>
                            <div class="col-md-2">
                                <button type="button" class="btn btn-outline-danger task-node-bash-stdout-delete"><i class="fa fa-times mr-1"></i> 删除</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!--如果任务类型是SQL-->
            <div class="task-node-sql" style="display: none">
                <!--选择数据源-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">数据源</label>
                    <div class="col-md-5">
                        <div class="select2-info">
                            <select class="select2 task-node-sql-data-source" data-dropdown-css-class="select2-info"
                                    data-placeholder="请选择数据源" style="width: 100%;"></select>
                        </div>
                    </div>
                </div>
                <!--选择SQL-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">执行SQL</label>
                    <div class="col-md-5">
                        <div class="select2-info">
                            <select class="select2 task-node-sql-script" data-dropdown-css-class="select2-info"
                                    data-placeholder="请选择执行脚本" style="width: 100%;"></select>
                        </div>
                    </div>
                </div>
                <!--SQL预览-->
                <div class="form-group row task-node-sql-text" style="display: none">
                    <label class="col-md-1 col-form-label"></label>
                    <div class="col-md-11"><textarea></textarea></div>
                </div>
                <!--SQL参数-->
                <div class="task-node-sql-stdin" style="display: none">
                    <div class="form-group row task-node-sql-stdin-item" data-task-node-sql-stdin-id="{{script-arg-id}}">
                        <label class="col-md-1 col-form-label">参数 {{script-arg-name}}</label>
                        <div class="col-md-5">
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <div class="btn-group btn-group-toggle task-node-sql-stdin-type" data-toggle="buttons">
                                        <label style="font-weight: normal" class="btn bg-info" data-task-node-sql-stdin-type-name="{{script-arg-type-name}}">
                                            <input type="radio" name="{{task-node-id}}-{{script-arg-id}}" autocomplete="off"> {{script-arg-type-value}}
                                        </label>
                                    </div>
                                    <input class="form-control task-node-sql-stdin-input" style="width: 300px; display: none">
                                    <select class="form-control task-node-sql-stdin-select custom-select" style="width: 300px; display: none"></select>
                                </div>
                            </div>
                        </div>
                        <label class="col-md-6 col-form-label text-gray font-weight-light">{{script-arg-description}}</label>
                    </div>
                </div>
                <!--结果解析-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">结果解析</label>
                    <div class="col-md-5">
                        <button type="button" class="btn btn-outline-info task-node-sql-stdout-add">
                            <i class="fa fa-plus"></i> 新增
                        </button>
                    </div>
                </div>
                <!--结果解析列表-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label"></label>
                        <div class="col-md-11 task-node-sql-stdout-list">
                            <div class="row task-node-sql-stdout-item" data-task-node-sql-stdout-index="{{task-node-sql-stdout-index}}">
                                <div class="col-md-4">
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text bg-info">模板参数</span>
                                        </div>
                                        <select class="form-control custom-select task-node-sql-stdout-select"></select>
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text bg-info">表达式</span>
                                        </div>
                                        <input type="text" class="form-control task-node-sql-stdout-expression">
                                    </div>
                                </div>
                                <div class="col-md-2">
                                    <button type="button" class="btn btn-outline-danger task-node-sql-stdout-delete"><i class="fa fa-times mr-1"></i> 删除</button>
                                </div>
                            </div>
                    </div>
                </div>
            </div>
            <!--如果任务类型是上传文件-->
            <div class="task-node-file" style="display: none">
                <!--任务目标-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">任务目标</label>
                    <div class="col-md-5">
                        <div class="select2-info">
                            <select class="select2 task-node-file-target" data-dropdown-css-class="select2-info"
                                    data-placeholder="请选择任务目标服务器" style="width: 100%;"></select>
                        </div>
                    </div>
                </div>
                <!--选择文件-->
                <div class="form-group row">
                    <label class="col-md-1 col-form-label">选择文件</label>
                    <div class="col-md-5">
                        <div class="select2-info">
                            <select class="select2 task-node-file-file" data-dropdown-css-class="select2-info"
                                    data-placeholder="请选择要上传的文件" style="width: 100%;"></select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="clearfix">
    <button id="task-save" type="button" class="btn btn-danger pull-right ml-1"><i class="fa fa-check"></i> 保存</button>
    <button id="task-node-add" type="button" class="btn btn-info pull-right"><i class="fa fa-plus"></i> 添加任务</button>
</div>

<script src="../../plugins/jquery/jquery.min.js"></script>
<script src="../../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="../../plugins/select2/js/select2.full.min.js"></script>
<script src="../../plugins/ion-rangeslider/js/ion.rangeSlider.min.js"></script>
<script src="../../plugins/bootstrap-slider/bootstrap-slider.min.js"></script>
<script src="../../plugins/codemirror/codemirror.js"></script>
<script src="../../plugins/codemirror/mode/sql/sql.js"></script>
<script src="../../plugins/codemirror/mode/shell/shell.js"></script>
<script src="../../plugins/codemirror/mode/javascript/javascript.js"></script>
<script src="../../adminLTE/js/adminlte.min.js"></script>
<script src="../js/common.js"></script>
<script src="../js/child.js"></script>
<script>
    $("#count").ionRangeSlider({min: 1, max: 10, step: 1, from: 1, type: "single", postfix: "台", grid: false});
    var countSlider = $("#count").data("ionRangeSlider");

    var params = Utils.getParams(location.href);
    var paramAction = params["action"];
    var paramId = params["id"];

    var data = {}
    if (paramAction === "modify" || paramAction === "detail") {
        $.ajax({
            url: Url.automationService.autoTaskTemplateDetail.replace("{id}", paramId),
            type: HttpMethod.get,
            async: false,
            success: function (result, status, xhr) {
                data = result;
            }
        });
    }

    var argTemplate,
        nodeTemplate,
        nodeBashStdinTemplate,
        nodeBashStdinTypeTemplate,
        nodeBashStdoutTemplate,
        nodeSqlStdinTemplate,
        nodeSqlStdinTypeTemplate,
        nodeSqlStdoutTemplate;

    var select2NodeTypes = [],
        select2NodeBashTargets = [],
        select2NodeBashScripts = [],
        select2NodeBashStdoutTypes = [],
        select2NodeSqlDataSources = [],
        select2NodeSqlScripts = [],
        select2NodeFileTargets = [],
        select2NodeFiles = [];

    var codeMirrorNodeBashTexts = [],
        codeMirrorNodeSqlTexts = [];

    var getUuid = function () {
        function S4() {
            return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
        }
        return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
    }
    var getArgTempalte = function () {
        argTemplate = $("#task-arg").html();
        $("#task-arg").html("");
    };
    var getNodeTemplate = function () {
        nodeTemplate = $("#task-node").html();
        $("#task-node").html("");
    };
    var getNodeBashStdinTemplate = function () {
        nodeBashStdinTemplate = $(".task-node-bash-stdin").html();
        $(".task-node-bash-stdin").html("");
    };
    var getNodeBashStdinTypeTemplate = function () {
        nodeBashStdinTypeTemplate = $(".task-node-bash-stdin-type").html();
        $(".task-node-bash-stdin-type").html("");
    };
    var getNodeBashStdoutTemplate = function () {
        nodeBashStdoutTemplate = $(".task-node-bash-stdout-list").html();
        $(".task-node-bash-stdout-list").html("");
    };
    var getNodeSqlStdinTemplate = function () {
        nodeSqlStdinTemplate = $(".task-node-sql-stdin").html();
        $(".task-node-sql-stdin").html("");
    };
    var getNodeSqlStdinTypeTemplate = function () {
        nodeSqlStdinTypeTemplate = $(".task-node-sql-stdin-type").html();
        $(".task-node-sql-stdin-type").html("");
    };
    var getNodeSqlStdoutTemplate = function () {
        nodeSqlStdoutTemplate = $(".task-node-sql-stdout-list").html();
        $(".task-node-sql-stdout-list").html("");
    };
    var getNodeTargetSelect2Date = function () {
        var count = $("#count").val();
        var data = [];
        for (let i = 0; i < count; i++) {
            data.push({index: i, name: "服务器" + (i + 1)});
        }
        return data;
    };
    var getNodeArgSelectOpt = function () {
        var argOpt = "";
        $("#task-arg").find(".task-arg-item").each(function (i, n){
            var argName = $(n).find(".task-arg-name").val();
            var argId = $(n).attr("data-task-arg-id")
            if (Utils.notEmpty(argName)) {
                argOpt += "<option value=\"" + argId + "\">" + argName +"</option>";
            }
        });
        return argOpt;
    };
    var addTaskArg = function () {
        var index = $("#task-arg .task-arg-item").length + 1;
        var argData = data.args && data.args.length >= index ? data.args[index - 1] : {};
        argData.id = argData.id ? argData.id : getUuid();

        $("#task-arg").append(argTemplate
            .replace(new RegExp("{{task-arg-id}}", "gm"), argData.id)
            .replace(new RegExp("{{task-arg-index}}", "gm"), index)
        );

        $.ajax({
            url: Url.automationService.autoTaskTemplateArgType,
            type: HttpMethod.get,
            async: false,
            headers: {pageSize: MaxPageSize, pageNumber: 1},
            success: function (result, status, xhr) {
                new Select2Ctrl("#task-arg .task-arg-item[data-task-arg-index=" + index + "] .task-arg-type",
                    result.records, "name", "value", "select").init(argData.type);
            }
        });

        $("#task-arg .task-arg-item[data-task-arg-index=" + index + "] .task-arg-name").val(argData.name);
        $("#task-arg .task-arg-item[data-task-arg-index=" + index + "] .task-arg-description").val(argData.description);
    };
    var addTaskNode = function () {
        var index = $("#task-node .task-node-item").length + 1;
        var nodeData = data.nodes && data.nodes.length >= index ? data.nodes[index - 1] : {};
        nodeData.id = nodeData.id ? nodeData.id : getUuid();

        $("#task-node").append(nodeTemplate
            .replace(new RegExp("{{task-node-id}}", "gm"), nodeData.id)
            .replace(new RegExp("{{task-node-index}}", "gm"), index)
        );

        $("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-name").val(nodeData.name);

        // 任务类型
        $.ajax({
            url: Url.automationService.autoTaskTemplateNodeType,
            type: HttpMethod.get,
            async: false,
            headers: {pageSize: MaxPageSize, pageNumber: 1},
            success: function (result, status, xhr) {
                var sl2NodeType = new Select2Ctrl("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-type",
                    result.records, "name", "value", "select2");
                select2NodeTypes.push(sl2NodeType);
                sl2NodeType.init(nodeData.type);
            }
        });

        // BASH节点CodeMirror
        var codeMirrorBashText = CodeMirror.fromTextArea($("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-bash-text textarea")[0], {
            mode: "application/json",
            theme: "darcula"
        });
        codeMirrorBashText.setSize("auto", "auto");
        codeMirrorBashText.setOption("readOnly", true);
        codeMirrorNodeBashTexts.push(codeMirrorBashText);

        // BASH节点任务目标
        var sl2BashTarget = new Select2Ctrl("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-bash-target",
            getNodeTargetSelect2Date(), "index", "name", "select2");
        select2NodeBashTargets.push(sl2BashTarget);
        sl2BashTarget.init(nodeData.serverIndex);

        // BASH节点脚本
        $.ajax({
            url: Url.baseService.scriptPage,
            type: HttpMethod.get,
            async: false,
            headers: {pageSize: MaxPageSize, pageNumber: 1},
            success: function (result, status, xhr) {
                var sl2BashScript = new Select2Ctrl("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-bash-script",
                    result.records, "id", "name", "select2");
                select2NodeBashScripts.push(sl2BashScript);
                sl2BashScript.init(nodeData.type === "BASH" ? nodeData.scriptId : undefined);
            }
        });

        // BASH节点STDOUT类型
        $.ajax({
            url: Url.automationService.autoTaskTemplateStdoutType,
            type: HttpMethod.get,
            async: false,
            headers: {pageSize: MaxPageSize, pageNumber: 1},
            success: function (result, status, xhr) {
                var sl2BashStdoutType = new Select2Ctrl("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-bash-stdout-type",
                    result.records, "name", "value", "select2");
                select2NodeBashStdoutTypes.push(sl2BashStdoutType);
                sl2BashStdoutType.init(nodeData.type === "BASH" ? nodeData.stdoutType : undefined);
            }
        });

        // SQL节点CodeMirror
        var codeMirrorSqlText = CodeMirror.fromTextArea($("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-sql-text textarea")[0], {
            mode: "application/json",
            theme: "darcula"
        });
        codeMirrorSqlText.setSize("auto", "auto");
        codeMirrorSqlText.setOption("readOnly", true);
        codeMirrorNodeSqlTexts.push(codeMirrorSqlText);

        // SQL节点数据源
        $.ajax({
            url: Url.dataService.dataSourcePage,
            type: HttpMethod.get,
            async: false,
            headers: {pageSize: MaxPageSize, pageNumber: 1},
            success: function (result, status, xhr) {
                var sl2SqlDS = new Select2Ctrl("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-sql-data-source",
                    result.records, "id", "name", "select2");
                select2NodeSqlDataSources.push(sl2SqlDS);
                sl2SqlDS.init(nodeData.type === "SQL" ? nodeData.dataSourceId : undefined);
            }
        });

        // SQL节点脚本
        $.ajax({
            url: Url.baseService.scriptPage,
            type: HttpMethod.get,
            async: false,
            headers: {pageSize: MaxPageSize, pageNumber: 1},
            success: function (result, status, xhr) {
                var sl2SqlScript = new Select2Ctrl("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-sql-script",
                    result.records, "id", "name", "select2");
                select2NodeSqlScripts.push(sl2SqlScript);
                sl2SqlScript.init(nodeData.type === "SQL" ? nodeData.scriptId : undefined);
            }
        });

        // FILE节点任务目标
        var sl2FileTarget = new Select2Ctrl("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-file-target",
            getNodeTargetSelect2Date(), "index", "name", "select2");
        select2NodeFileTargets.push(sl2FileTarget);
        sl2FileTarget.init(nodeData.serverIndex);

        // FILE节点文件
        $.ajax({
            url: Url.fileList,
            type: HttpMethod.get,
            async: false,
            headers: {pageSize: MaxPageSize, pageNumber: 1},
            success: function (result, status, xhr) {
                var sl2File = new Select2Ctrl("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-file-file",
                    result, "name", "name", "select2");
                select2NodeFiles.push(sl2File);
                sl2File.init(nodeData.type === "FILE" ? nodeData.fileId : undefined);
            }
        });

        if (nodeData.stdout && nodeData.stdout.length > 0) {
            for (let i = 0; i < nodeData.stdout.length; i++) {
                if (nodeData.type === "BASH") {
                    $("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-bash-stdout-add").click();
                } else if (nodeData.type === "SQL") {
                    $("#task-node .task-node-item[data-task-node-index=" + index + "] .task-node-sql-stdout-add").click();
                }
            }
        }
    };
    var selectBashScript = function (dom) {
        var scriptId = $(dom).val();
        if (Utils.isEmpty(scriptId)) return;
        var $node = $(dom).parents(".task-node-item");
        var nodeIndex = $node.attr("data-task-node-index");
        var nodeId = $node.attr("data-task-node-id");
        var nodeData = data.nodes && data.nodes.length >= nodeIndex ? data.nodes[nodeIndex - 1] : {};
        var $stdin = $node.find(".task-node-bash-stdin");
        $.ajax({
            url: Url.baseService.scriptDetail.replace("{id}", scriptId),
            type: HttpMethod.get,
            async: false,
            success: function (scriptResult, status, xhr) {
                var $text = $("#task-node").find(".task-node-item[data-task-node-index=" + nodeIndex + "]")
                    .find(".task-node-bash-text").css("display", "");
                var codeMirror = codeMirrorNodeBashTexts[nodeIndex - 1];
                var type = scriptResult["type"];
                if ("SQL" === type) {
                    codeMirror.setOption("mode", "text/x-sql");
                } else if ("BASH" === type) {
                    codeMirror.setOption("mode", "text/x-sh");
                } else {
                    codeMirror.setOption("mode", "application/json");
                }
                codeMirror.setValue(scriptResult.text);

                if (scriptResult["args"]) {
                    $stdin.css("display", "").html("");
                    for (let i = 0; i < scriptResult["args"].length; i++) {
                        var argData = {};
                        for (let j = 0; j < (nodeData.stdin ? nodeData.stdin.length : 0); j++) {
                            if (nodeData.stdin[j].scriptArgId === scriptResult["args"][i]["id"]) {
                                argData = nodeData.stdin[j];
                                break;
                            }
                        }

                        $stdin.append(nodeBashStdinTemplate
                            .replace(new RegExp("{{script-arg-id}}", "gm"), scriptResult["args"][i]["id"])
                            .replace(new RegExp("{{script-arg-name}}", "gm"), scriptResult["args"][i]["name"])
                            .replace(new RegExp("{{script-arg-description}}", "gm"), scriptResult["args"][i]["description"])
                        );
                        var $stdinItem = $stdin.find(".task-node-bash-stdin-item[data-task-node-bash-stdin-id=" + scriptResult["args"][i]["id"] + "]");
                        $stdinItem.find(".task-node-bash-stdin-select").html(getNodeArgSelectOpt()).val(argData.autoTaskTemplateArgId).trigger("change");
                        $stdinItem.find(".task-node-bash-stdin-input").val(argData.value);
                        $.ajax({
                            url: Url.automationService.autoTaskTemplateStdinType,
                            type: HttpMethod.get,
                            async: false,
                            success: function (stdinTypeResult, status, xhr) {
                                for (let j = 0; j < stdinTypeResult["records"].length; j++) {
                                    $stdinItem.find(".task-node-bash-stdin-type").append(nodeBashStdinTypeTemplate
                                        .replace(new RegExp("{{task-node-id}}", "gm"), nodeId)
                                        .replace(new RegExp("{{script-arg-id}}", "gm"), scriptResult["args"][i]["id"])
                                        .replace(new RegExp("{{script-arg-name}}", "gm"), scriptResult["args"][i]["name"])
                                        .replace(new RegExp("{{script-arg-description}}", "gm"), scriptResult["args"][i]["description"])
                                        .replace(new RegExp("{{script-arg-type-name}}", "gm"), stdinTypeResult["records"][j]["name"])
                                        .replace(new RegExp("{{script-arg-type-value}}", "gm"), stdinTypeResult["records"][j]["value"])
                                    );
                                }
                                var stdinTypeSelect = argData.type ? argData.type : stdinTypeResult["records"][0]["name"];
                                $stdinItem.find(".task-node-bash-stdin-type label[data-task-node-bash-stdin-type-name=" + stdinTypeSelect  + "]").addClass("active").click();
                            }
                        });
                    }
                }
            }
        });
    };
    var selectSqlScript = function (dom) {
        var scriptId = $(dom).val();
        if (Utils.isEmpty(scriptId)) return;
        var $node = $(dom).parents(".task-node-item");
        var nodeIndex = $node.attr("data-task-node-index");
        var nodeData = data.nodes && data.nodes.length >= nodeIndex ? data.nodes[nodeIndex - 1] : {};
        var $stdin = $node.find(".task-node-sql-stdin");
        $.ajax({
            url: Url.baseService.scriptDetail.replace("{id}", scriptId),
            type: HttpMethod.get,
            async: false,
            success: function (scriptResult, status, xhr) {
                var $text = $("#task-node").find(".task-node-item[data-task-node-index=" + nodeIndex + "]")
                    .find(".task-node-sql-text").css("display", "");
                var codeMirror = codeMirrorNodeSqlTexts[nodeIndex - 1];
                var type = scriptResult["type"];
                if ("SQL" === type) {
                    codeMirror.setOption("mode", "text/x-sql");
                } else if ("BASH" === type) {
                    codeMirror.setOption("mode", "text/x-sh");
                } else {
                    codeMirror.setOption("mode", "application/json");
                }
                codeMirror.setValue(scriptResult.text);

                if (scriptResult["args"]) {
                    $stdin.css("display", "").html("");
                    for (let i = 0; i < scriptResult["args"].length; i++) {
                        var argData = {};
                        for (let j = 0; j < (nodeData.stdin ? nodeData.stdin.length : 0); j++) {
                            if (nodeData.stdin[j].scriptArgId === scriptResult["args"][i]["id"]) {
                                argData = nodeData.stdin[j];
                                break;
                            }
                        }

                        $stdin.append(nodeSqlStdinTemplate
                            .replace(new RegExp("{{script-arg-id}}", "gm"), scriptResult["args"][i]["id"])
                            .replace(new RegExp("{{script-arg-name}}", "gm"), scriptResult["args"][i]["name"])
                            .replace(new RegExp("{{script-arg-description}}", "gm"), scriptResult["args"][i]["description"])
                        );
                        var $stdinItem = $stdin.find(".task-node-sql-stdin-item[data-task-node-sql-stdin-id=" + scriptResult["args"][i]["id"] + "]");
                        $stdinItem.find(".task-node-sql-stdin-select").html(getNodeArgSelectOpt()).val(argData.autoTaskTemplateArgId).trigger("change");
                        $stdinItem.find(".task-node-sql-stdin-input").val(argData.value);
                        $.ajax({
                            url: Url.automationService.autoTaskTemplateStdinType,
                            type: HttpMethod.get,
                            async: false,
                            success: function (stdinTypeResult, status, xhr) {
                                for (let j = 0; j < stdinTypeResult["records"].length; j++) {
                                    $stdinItem.find(".task-node-sql-stdin-type").append(nodeSqlStdinTypeTemplate
                                        .replace(new RegExp("{{script-arg-id}}", "gm"), scriptResult["args"][i]["id"])
                                        .replace(new RegExp("{{script-arg-name}}", "gm"), scriptResult["args"][i]["name"])
                                        .replace(new RegExp("{{script-arg-description}}", "gm"), scriptResult["args"][i]["description"])
                                        .replace(new RegExp("{{script-arg-type-name}}", "gm"), stdinTypeResult["records"][j]["name"])
                                        .replace(new RegExp("{{script-arg-type-value}}", "gm"), stdinTypeResult["records"][j]["value"])
                                    );
                                }
                                var stdinTypeSelect = argData.type ? argData.type : stdinTypeResult["records"][0]["name"];
                                $stdinItem.find(".task-node-sql-stdin-type label[data-task-node-sql-stdin-type-name=" + stdinTypeSelect  + "]").addClass("active").click();
                            }
                        });
                    }
                }
            }
        });
    };
    var selectDataSource = function (dom) {
        var id = $(dom).val();
        var nodeIndex = $(dom).parents(".task-node-item").attr("data-task-node-index");
        var nodeData = data.nodes && data.nodes.length >= nodeIndex ? data.nodes[nodeIndex - 1] : {};
        var select2DataSource = select2NodeSqlDataSources[nodeIndex - 1];
    };
    var addBashStdout = function (dom) {
        var nodeIndex = $(dom).parents(".task-node-item").attr("data-task-node-index");
        var nodeData = data.nodes && data.nodes.length >= nodeIndex ? data.nodes[nodeIndex - 1] : {};
        var stdoutIndex = $(dom).parents(".task-node-item").find(".task-node-bash-stdout-item").length + 1;
        var stdoutData = nodeData.stdout && nodeData.stdout.length >= stdoutIndex ? nodeData.stdout[stdoutIndex - 1] : {};

        var $stdoutList = $(dom).parents(".task-node-item").find(".task-node-bash-stdout-list");
        $stdoutList.append(nodeBashStdoutTemplate
            .replace(new RegExp("{{task-node-bash-stdout-index}}", "gm"), (stdoutIndex))
        );
        var $stdoutItem =  $(dom).parents(".task-node-item")
            .find(".task-node-bash-stdout-item[data-task-node-bash-stdout-index=" + (stdoutIndex) + "]");
        $stdoutItem.find(".task-node-bash-stdout-select").html(getNodeArgSelectOpt()).val(stdoutData.autoTaskTemplateArgId);
        $stdoutItem.find(".task-node-bash-stdout-expression").val(stdoutData.expression);
    };
    var addSqlStdout = function (dom) {
        var nodeIndex = $(dom).parents(".task-node-item").attr("data-task-node-index");
        var nodeData = data.nodes && data.nodes.length >= nodeIndex ? data.nodes[nodeIndex - 1] : {};
        var stdoutIndex = $(dom).parents(".task-node-item").find(".task-node-sql-stdout-item").length + 1;
        var stdoutData = nodeData.stdout && nodeData.stdout.length >= stdoutIndex ? nodeData.stdout[stdoutIndex - 1] : {};

        var $stdoutList = $(dom).parents(".task-node-item").find(".task-node-sql-stdout-list");
        $stdoutList.append(nodeSqlStdoutTemplate
            .replace(new RegExp("{{task-node-sql-stdout-index}}", "gm"), (stdoutIndex))
        );
        var $stdoutItem =  $(dom).parents(".task-node-item")
            .find(".task-node-sql-stdout-item[data-task-node-sql-stdout-index=" + (stdoutIndex) + "]");
        $stdoutItem.find(".task-node-sql-stdout-select").html(getNodeArgSelectOpt()).val(stdoutData.autoTaskTemplateArgId);
        $stdoutItem.find(".task-node-sql-stdout-expression").val(stdoutData.expression);
    };
    var checkRequireSelect2 = function (dom) {

        if (Utils.isEmpty($(dom).val())) {
            $(dom).attr("data-dropdown-css-class", "select2-danger")
                .parent().removeClass("select2-info").addClass("select2-danger");
            return false;
        } else {
            $(dom).attr("data-dropdown-css-class", "select2-info")
                .parent().removeClass("select2-danger").addClass("select2-info");
            return true;
        }
    };
    var checkRequireInput = function (dom) {
        if (Utils.isEmpty($(dom).val())) {
            $(dom).removeClass("is-valid").addClass("is-invalid");
            return false;
        } else {
            $(dom).removeClass("is-invalid").addClass("is-valid");
            return true;
        }
    };
    var checkRequire = function (dom) {
        if ($(dom).hasClass("select2")) {
            return checkRequireSelect2(dom);
        } else {
            return checkRequireInput(dom);
        }
    }
    var checkData = function () {
        var validate = true;
        validate = checkRequire($("#name")) && validate;
        $("#task-arg > .task-arg-item").each(function (argIndex, argDom) {
            validate = checkRequire($(argDom).find(".task-arg-type")) && validate;
            validate = checkRequire($(argDom).find(".task-arg-name")) && validate;
            validate = checkRequire($(argDom).find(".task-arg-description")) && validate;
        });
        $("#task-node .task-node-item").each(function (nodeIndex, nodeDom) {
            validate = checkRequire($(nodeDom).find(".task-node-name")) && validate;
            validate = checkRequire($(nodeDom).find(".task-node-type")) && validate;

            if ($(nodeDom).find(".task-node-type").val() === "BASH") {
                validate = checkRequire($(nodeDom).find(".task-node-bash-target")) && validate;
                validate = checkRequire($(nodeDom).find(".task-node-bash-script")) && validate;
                $(nodeDom).find(".task-node-bash-stdin").find(".task-node-bash-stdin-item").each(function (stdinIndex, stdinObj){
                    if ("ARG" === $(stdinObj).find(".task-node-bash-stdin-type > label.active").attr("data-task-node-bash-stdin-type-name")) {
                        validate = checkRequire($(stdinObj).find(".task-node-bash-stdin-select")) && validate;
                    } else {
                        validate = checkRequire($(stdinObj).find(".task-node-bash-stdin-input")) && validate;
                    }
                });
                validate = checkRequire($(nodeDom).find(".task-node-bash-stdout-type")) && validate;
                $(nodeDom).find(".task-node-bash-stdout-list").find(".task-node-bash-stdout-item").each(function (stdoutIndex, stdoutObj){
                    validate = checkRequire($(stdoutObj).find(".task-node-bash-stdout-select")) && validate;
                    validate = checkRequire($(stdoutObj).find(".task-node-bash-stdout-expression")) && validate;
                });
            } else if ($(nodeDom).find(".task-node-type").val() === "SQL") {
                validate = checkRequire($(nodeDom).find(".task-node-sql-script")) && validate;
                validate = checkRequire($(nodeDom).find(".task-node-sql-data-source")) && validate;
                $(nodeDom).find(".task-node-sql-stdin").find(".task-node-sql-stdin-item").each(function (stdinIndex, stdinObj){
                    if ("ARG" === $(stdinObj).find(".task-node-sql-stdin-type > label.active").attr("data-task-node-sql-stdin-type-name")) {
                        validate = checkRequire($(stdinObj).find(".task-node-sql-stdin-select")) && validate;
                    } else {
                        validate = checkRequire($(stdinObj).find(".task-node-sql-stdin-input")) && validate;
                    }
                });
                $(nodeDom).find(".task-node-sql-stdout-list").find(".task-node-sql-stdout-item").each(function (stdoutIndex, stdoutObj){
                    validate = checkRequire($(stdoutObj).find(".task-node-sql-stdout-select")) && validate;
                    validate = checkRequire($(stdoutObj).find(".task-node-sql-stdout-expression")) && validate;
                });
            } else if ($(nodeDom).find(".task-node-type").val() === "FILE") {
                validate = checkRequire($(nodeDom).find(".task-node-file-target")) && validate;
                validate = checkRequire($(nodeDom).find(".task-node-file-file")) && validate;
            }
        });
        return validate;
    }
    var getData = function () {
        var data = {};
        data.id = $("#id").val();
        data.name = $("#name").val();
        data.serverCount = $("#count").val();
        data.args = [];
        $("#task-arg > .task-arg-item").each(function (argIndex, nodeDom) {
            var arg = {};
            arg.id = $(nodeDom).attr("data-task-arg-id");
            arg.type = $(nodeDom).find(".task-arg-type").val();
            arg.name = $(nodeDom).find(".task-arg-name").val();
            arg.description = $(nodeDom).find(".task-arg-description").val();
            data.args.push(arg);
        });
        data.nodes = [];
        $("#task-node .task-node-item").each(function (nodeIndex, nodeDom) {
            var node = {};
            node.id = $(nodeDom).attr("data-task-node-id")
            node.name = $(nodeDom).find(".task-node-name").val();
            node.type = $(nodeDom).find(".task-node-type").val();
            if (node.type === "BASH") {
                node.serverIndex = $(nodeDom).find(".task-node-bash-target").val();
                node.scriptId = $(nodeDom).find(".task-node-bash-script").val();
                node.stdin = [];
                $(nodeDom).find(".task-node-bash-stdin").find(".task-node-bash-stdin-item").each(function (stdinIndex, stdinObj){
                    var stdin = {};
                    stdin.scriptArgId = $(stdinObj).attr("data-task-node-bash-stdin-id");
                    stdin.type = $(stdinObj).find(".task-node-bash-stdin-type > label.active").attr("data-task-node-bash-stdin-type-name")
                    if ("ARG" === stdin.type) {
                        stdin.autoTaskTemplateArgId = $(stdinObj).find(".task-node-bash-stdin-select").val();
                    } else {
                        stdin.value = $(stdinObj).find(".task-node-bash-stdin-input").val();
                    }
                    node.stdin.push(stdin);
                });
                node.stdoutType = $(nodeDom).find(".task-node-bash-stdout-type").val();
                node.stdout = [];
                $(nodeDom).find(".task-node-bash-stdout-list").find(".task-node-bash-stdout-item").each(function (stdoutIndex, stdoutObj){
                    var stdout = {};
                    stdout.autoTaskTemplateArgId = $(stdoutObj).find(".task-node-bash-stdout-select").val();
                    stdout.expression = $(stdoutObj).find(".task-node-bash-stdout-expression").val();
                    node.stdout.push(stdout);
                });
            } else if (node.type === "SQL") {
                node.dataSourceId = $(nodeDom).find(".task-node-sql-data-source").val();
                node.scriptId = $(nodeDom).find(".task-node-sql-script").val();
                node.stdin = [];
                $(nodeDom).find(".task-node-sql-stdin").find(".task-node-sql-stdin-item").each(function (stdinIndex, stdinObj){
                    var stdin = {};
                    stdin.scriptArgId = $(stdinObj).attr("data-task-node-sql-stdin-id");
                    stdin.type = $(stdinObj).find(".task-node-sql-stdin-type > label.active").attr("data-task-node-sql-stdin-type-name")
                    if ("ARG" === stdin.type) {
                        stdin.autoTaskTemplateArgId = $(stdinObj).find(".task-node-sql-stdin-select").val();
                    } else {
                        stdin.value = $(stdinObj).find(".task-node-sql-stdin-input").val();
                    }
                    node.stdin.push(stdin);
                });
                node.stdoutType = $(nodeDom).find(".task-node-sql-stdout-type").val();
                node.stdout = [];
                $(nodeDom).find(".task-node-sql-stdout-list").find(".task-node-sql-stdout-item").each(function (stdoutIndex, stdoutObj){
                    var stdout = {};
                    stdout.autoTaskTemplateArgId = $(stdoutObj).find(".task-node-sql-stdout-select").val();
                    stdout.expression = $(stdoutObj).find(".task-node-sql-stdout-expression").val();
                    node.stdout.push(stdout);
                });
            } else if (node.type === "FILE") {
                node.serverIndex = $(nodeDom).find(".task-node-file-target").val();
                node.fileId = $(nodeDom).find(".task-node-file-file").val();
            }
            data.nodes.push(node);
        });
        return data;
    };
    var setData = function () {
        $("#id").val(data.id);
        $("#name").val(data.name);

        $("#count").val(data.serverCount);
        countSlider.update({from: data.serverCount});
        countSlider.reset();

        if (data.args) {
            for (let i = 0; i < data.args.length; i++) {
                $("#task-arg-add").click();
            }
        }

        if (data.nodes) {
            for (let i = 0; i < data.nodes.length; i++) {
                $("#task-node-add").click();
            }
        }
    };
    var bindEvent = function () {
        // 添加模板参数
        $("#task-arg-add").click(function (){
            addTaskArg();
        });
        // 删除模板参数
        $("#task-arg").on("click", ".task-arg-del", function () {
            $(this).parents(".row").remove();
        });
        // 输入模板参数时调整BASH节点和SQL节点的参数下拉
        $("#task-arg").on("change", ".task-arg-name", function () {
            var argOpt = getNodeArgSelectOpt();
            $("#task-node .task-node-bash-stdin-select").html(argOpt);
            $("#task-node .task-node-sql-stdin-select").html(argOpt);
            $("#task-node .task-node-bash-stdout-select").html(argOpt);
            $("#task-node .task-node-sql-stdout-select").html(argOpt);
        });
        // 输入服务器数量时调整任务目标数量
        $("#count").change(function () {
            var datas = getNodeTargetSelect2Date();
            for (let i = 0; i < select2NodeBashTargets.length; i++) {
                select2NodeBashTargets[i].initByData(datas, $(select2NodeBashTargets[i].id).val());
            }
            for (let i = 0; i < select2NodeFileTargets.length; i++) {
                select2NodeFileTargets[i].initByData(datas, $(select2NodeFileTargets[i].id).val());
            }
        })
        // 添加任务节点
        $("#task-node-add").click(function () {
            addTaskNode();
        });
        // 删除任务节点
        $("#task-node").on("click", ".task-node-delete", function () {
            var index = parseInt($(this).parents(".task-node-item").attr("data-task-node-index"));
            var length = $("#task-node").find(".task-node-item").length;

            for (let i = index + 1; i <= length ; i++) {
                $("#task-node").find(".task-node-item[data-task-node-index=" + i + "]").find(".task-node-item-index").html(i - 1);
                $("#task-node").find(".task-node-item[data-task-node-index=" + i + "]").attr("data-task-node-index", i - 1);

                select2NodeTypes[i - 1].id = select2NodeTypes[i - 1].id.replace("data-task-node-index=" + i, "data-task-node-index=" + (i - 1));
                select2NodeBashTargets[i - 1].id = select2NodeBashTargets[i - 1].id.replace("data-task-node-index=" + i, "data-task-node-index=" + (i - 1));
                select2NodeBashScripts[i - 1].id = select2NodeBashScripts[i - 1].id.replace("data-task-node-index=" + i, "data-task-node-index=" + (i - 1));
                select2NodeBashStdoutTypes[i - 1].id = select2NodeBashStdoutTypes[i - 1].id.replace("data-task-node-index=" + i, "data-task-node-index=" + (i - 1));
                select2NodeSqlDataSources[i - 1].id = select2NodeSqlDataSources[i - 1].id.replace("data-task-node-index=" + i, "data-task-node-index=" + (i - 1));
                select2NodeSqlScripts[i - 1].id = select2NodeSqlScripts[i - 1].id.replace("data-task-node-index=" + i, "data-task-node-index=" + (i - 1));
                select2NodeFileTargets[i - 1].id = select2NodeFileTargets[i - 1].id.replace("data-task-node-index=" + i, "data-task-node-index=" + (i - 1));
                select2NodeFiles[i - 1].id = select2NodeFiles[i - 1].id.replace("data-task-node-index=" + i, "data-task-node-index=" + (i - 1));
            }

            $(this).parents(".task-node-item").remove();
            select2NodeTypes.splice(index - 1, 1);
            select2NodeBashTargets.splice(index - 1, 1);
            select2NodeBashScripts.splice(index - 1, 1);
            select2NodeBashStdoutTypes.splice(index - 1, 1);
            select2NodeSqlDataSources.splice(index - 1, 1);
            select2NodeSqlScripts.splice(index - 1, 1);
            select2NodeFileTargets.splice(index - 1, 1);
            select2NodeFiles.splice(index - 1, 1);

            codeMirrorNodeBashTexts.splice(index - 1, 1);
            codeMirrorNodeSqlTexts.splice(index - 1, 1);
        });
        // 选择节点类型
        $("#task-node").on("change", ".task-node-type", function () {
            var type = $(this).val();
            var $item = $(this).parents(".task-node-item");
            $item.find(".task-node-bash").css("display", type === "BASH" ? "" : "none");
            $item.find(".task-node-sql").css("display", type === "SQL" ? "" : "none");
            $item.find(".task-node-file").css("display", type === "FILE" ? "" : "none");
        });
        // BASH节点选择脚本
        $("#task-node").on("change", ".task-node-bash-script", function () {
            selectBashScript(this);
        });
        // BASH节点点击STDIN参数类型
        $("#task-node").on("click", ".task-node-bash-stdin-type label", function () {
            var type = $(this).attr("data-task-node-bash-stdin-type-name");
            var $input = $(this).parents(".task-node-bash-stdin-item").find(".task-node-bash-stdin-input");
            var $select = $(this).parents(".task-node-bash-stdin-item").find(".task-node-bash-stdin-select");
            $input.css("display", type === "INPUT" ? "": "none");
            $select.css("display", type === "ARG" ? "": "none");
        });
        // BASH节点添加结果解析
        $("#task-node").on("click", ".task-node-bash-stdout-add", function () {
            addBashStdout(this);
        });
        // BASH节点删除结果解析
        $("#task-node").on("click", ".task-node-bash-stdout-delete", function () {
            $(this).parents(".task-node-bash-stdout-item").remove();
        });
        // SQL节点选择数据源
        $("#task-node").on("change", ".task-node-sql-data-source", function () {
            selectDataSource(this);
        });
        // SQL节点选择脚本
        $("#task-node").on("change", ".task-node-sql-script", function () {
            selectSqlScript(this);
        });
        // SQL节点点击STDIN参数类型
        $("#task-node").on("click", ".task-node-sql-stdin-type label", function () {
            var type = $(this).attr("data-task-node-sql-stdin-type-name");
            var $input = $(this).parents(".task-node-sql-stdin-item").find(".task-node-sql-stdin-input");
            var $select = $(this).parents(".task-node-sql-stdin-item").find(".task-node-sql-stdin-select");
            $input.css("display", type === "INPUT" ? "": "none");
            $select.css("display", type === "ARG" ? "": "none");
        });
        // SQL节点添加结果解析
        $("#task-node").on("click", ".task-node-sql-stdout-add", function () {
            addSqlStdout(this);
        });
        // SQL节点删除结果解析
        $("#task-node").on("click", ".task-node-sql-stdout-delete", function () {
            $(this).parents(".task-node-sql-stdout-item").remove();
        });
        // 保存按钮
        $("#task-save").click(function () {
            var validate = checkData();
            if (!validate) {
                return;
            }

            var data = getData();

            if (data.id) {
                $.ajax({
                    url: Url.automationService.autoTaskTemplateModify.replace("{id}", data.id),
                    type: HttpMethod.put,
                    data: JSON.stringify(data),
                    contentType: ContentType.json,
                    success: function (result, status, xhr) {
                        parent.window.frameCtrl.toastr.success("修改成功！");
                        parent.window.frameCtrl.windowCtrl.close();
                    }
                });
            } else {
                $.ajax({
                    url: Url.automationService.autoTaskTemplateCreate,
                    type: HttpMethod.post,
                    data: JSON.stringify(data),
                    contentType: ContentType.json,
                    success: function (result, status, xhr) {
                        parent.window.frameCtrl.toastr.success("创建成功！");
                        parent.window.frameCtrl.windowCtrl.close();
                    }
                });
            }

        });
    };

    getArgTempalte();
    getNodeBashStdinTypeTemplate();
    getNodeBashStdinTemplate()
    getNodeBashStdoutTemplate();
    getNodeSqlStdinTypeTemplate();
    getNodeSqlStdinTemplate();
    getNodeSqlStdoutTemplate();
    getNodeTemplate();
    bindEvent();

    setData();

    data = {};
</script>

</body>
</html>